package com.ylyq.ylxx.controller;

import com.ylyq.ylxx.constants.AccountConstant;
import com.ylyq.ylxx.model.ResponseData;
import com.ylyq.ylxx.model.bean.*;
import com.ylyq.ylxx.model.request.*;
import com.ylyq.ylxx.service.IOceaneginePlanService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName OceanenginePlanController
 * @Description TODO
 * @Author xiaweichao
 * @Date 2020/6/28 15:37
 * @Version 1.0
 */
@RestController
@RequestMapping("/oCeanengine/plan")
@Api(description = "巨量引擎广告计划的相关接口")
public class OceanenginePlanController {
    @Autowired
    private IOceaneginePlanService iOceaneginePlanService;

//    @Autowired
//    private IYlyqPlanInfoService iYlyqPlanInfoService;

    @PostMapping("/getPlan")
    @ApiOperation(value = "获取广告计划", response = OceanenginePlanInfo.class)
    public ResponseData getPlan(@Valid @RequestBody GetPlanInfoRequest getPlanInfoRequest) {
        return new ResponseData(ResponseData.Result.SUCCESS,iOceaneginePlanService.getPlan(getPlanInfoRequest));
    }

    @PostMapping("/getPlanAndReport")
    @ApiOperation(value = "获取广告计划", response = OceanenginePlanInfo.class)
    public ResponseData getPlanAndReport(@Valid @RequestBody GetPlanInfoRequest getPlanInfoRequest) {
        return iOceaneginePlanService.getPlanAndReport(getPlanInfoRequest);
    }

    @PostMapping("/createBatchPlan")
    @ApiOperation(value = "批量创建广告计划", response = OceanengineCreatePlanResult.class)
    public ResponseData createBatchPlan(@Valid @RequestBody List<CreatePlanInfoRequest> createGroupInfoRequest) {

        List<OceanengineCreatePlanResult> results = new ArrayList<>();
        for (CreatePlanInfoRequest temp : createGroupInfoRequest) {
            OceanengineCreatePlanResult groupResult = iOceaneginePlanService.createPlan(temp);
            results.add(groupResult);
//            if(groupResult.getCode()!= AccountConstant.SUCCESS){
////                YlyqPlanInfo ylyqPlanInfo = new YlyqPlanInfo();
////                ylyqPlanInfo.setPlanid(String.valueOf(groupResult.getData().getAd_id()));
////                ylyqPlanInfo.setChooseCodeUrl(temp.getChooseCodeUrl());
////                ylyqPlanInfo.setChooseUrl(temp.getChooseUrl());
////                iYlyqPlanInfoService.save(ylyqPlanInfo);
//            }
        }
        return new ResponseData(ResponseData.Result.SUCCESS, results);
    }

    @PostMapping("/updateBatchPlan")
    @ApiOperation(value = "批量修改广告计划", response = OceanengineUpdatePlanResult.class)
    public ResponseData updateBatchPlan(@Valid @RequestBody List<UpdatePlanInfoRequest> updatePlanInfoRequests) {

        List<OceanengineUpdatePlanResult> results = new ArrayList<>();
        for (UpdatePlanInfoRequest temp : updatePlanInfoRequests) {
            OceanengineUpdatePlanResult groupResult = iOceaneginePlanService.updatePlan(temp);
            results.add(groupResult);
        }
        return new ResponseData(ResponseData.Result.SUCCESS, results);
    }

    @PostMapping("/updateBatchPlanState")
    @ApiOperation(value = "更新广告计划状态", response = OceanengineUpdatePlanStateResult.class)
    public ResponseData updateBatchPlanState(@Valid @RequestBody UpdatePlanStateInfoRequest updatePlanInfoRequests) {
        OceanengineUpdatePlanStateResult groupResult = iOceaneginePlanService.updateBatchPlanState(updatePlanInfoRequests);
        return new ResponseData(ResponseData.Result.SUCCESS, groupResult);
    }

    @PostMapping("/updateBatchPlanBudget")
    @ApiOperation(value = "更新广告计划预算", response = OceanengineUpdatePlanBudgetResult.class)
    public ResponseData updateBatchPlanBudget(@Valid @RequestBody UpdatePlanBudgetInfoRequest updatePlanInfoRequests) {
        OceanengineUpdatePlanBudgetResult groupResult = iOceaneginePlanService.updateBatchPlanBudget(updatePlanInfoRequests);
        return new ResponseData(ResponseData.Result.SUCCESS, groupResult);
    }

    @PostMapping("/updateBatchPlanBid")
    @ApiOperation(value = "更新广告计划出价", response = OceanengineUpdatePlanBidResult.class)
    public ResponseData updateBatchPlanBid(@Valid @RequestBody UpdatePlanBidInfoRequest updatePlanBidInfoRequest) {
        OceanengineUpdatePlanBidResult groupResult = iOceaneginePlanService.updateBatchPlanBid(updatePlanBidInfoRequest);
        return new ResponseData(ResponseData.Result.SUCCESS, groupResult);
    }

    @PostMapping("/getRejectReason")
    @ApiOperation(value = "获取计划审核建议，只有审核不通过的计划才有审核建议，审核通过的计划没有审核建议。", response = OceanengineRejectReasonResult.class)
    public ResponseData getRejectReason(@Valid @RequestBody GetRejectReasonRequest getRejectReason) {
        OceanengineRejectReasonResult groupResult = iOceaneginePlanService.getRejectReason(getRejectReason);
        return new ResponseData(ResponseData.Result.SUCCESS, groupResult);
    }

    @PostMapping("/generatePlans")
    @ApiOperation(value = "批量生成广告计划",response = ResultPlan.class)
    public ResponseData generatePlans(@Valid @RequestBody GeneratePlansRequest generatePlansRequest) {
        try {
            return iOceaneginePlanService.generatePlans(generatePlansRequest);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseData(ResponseData.Result.FAIL);
        }
    }

}
